import ujson as json
import uos as os
import utime as time


class MsgProcess():

    def __init__(self):
        self.config = {}
        self.get_config()

    def get_config(self):
        try:
            os.stat('./data/config.json')
            with open('./data/config.json') as fp:
                self.config = json.load(fp)
            return self.config
        except OSError:
            return {
                'wificonf': 'data/wificonf.txt',
                'equipm': 'data/equipm.txt',
                'version': 'data/version.txt',
                'gpio': {'setnetpin': '','statusled': ''},
                'mqtt': {'host': '','port': 1883},
                'httpapi': ''
            }

    def is_network(self, outtime=5):
        import network
        time_i = 0
        sta_if = network.WLAN(network.STA_IF)
        is_net = True
        while not sta_if.isconnected():
            time.sleep(1)
            time_i += 1
            if time_i > outtime:
                is_net = False
                break
        return (is_net, sta_if) if is_net else False

    def is_filetype(self, path):
        if not path:return False
        try:
            os.listdir(path)
            return 'dir'
        except OSError:
            os.stat(path)
            return 'file'
        else:
            return ''

    def send(self, MsgType='Text', Receiver='WnkgProxy', Data=None, Sender=None):
        pass

    def Start(self, message=None):
        pass

    def Text(self, message):
        pass

